package MyApp::Controller::Workflow;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::JSON qw(decode_json encode_json);
use MyApp::Model::Translate qw(translateSbuId translateUserRole);
use Workflow::Factory qw( FACTORY );
use Encode;
use Log::Log4perl qw( get_logger );

sub get_my_workflow {
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    my $db = $self->mysql->db;

    #my $creator = "admin"; #此为测试句，正式发布前应改为下面一句：
    my $creator = $self->session('user');

    my $hashes = $db->query('select number,workflow_id,workflow_type,agree_user,item from daiban where is_finish=0 and creator=?',$creator)->hashes;

    if ($hashes) {
        return $self->render(json => { value => 1, info => $hashes });
    }else{
        return $self->render(json => { value => 0 });
    }

}

sub get_lastfinish {
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    my $db = $self->mysql->db;

    #my $creator = "admin"; #此为测试句，正式发布前应改为下面一句：
    my $creator = $self->session('user');

    my $hashes1 = $db->query("select workflow_id from wf_history_company where to_days(now())-to_days(history_date)<=1 and state='JIESHU'")->hashes;
    my $hashes2 = $db->query("select workflow_id from wf_history_account where to_days(now())-to_days(history_date)<=1 and state='JIESHU'")->hashes;
    my $hashes3 = $db->query("select workflow_id from wf_history_mac where to_days(now())-to_days(history_date)<=1 and state='JIESHU'")->hashes;
    
    my @array;

    for my $hash1 (@$hashes1) {
        my $hash = $db->query('select number,workflow_id,workflow_type,agree_user,item from daiban where workflow_type=1 and workflow_id=? and (creator=? or agree_user=?)',$hash1->{workflow_id},$creator,$creator)->hash;
       if ($hash->{number}) {
           push @array,$hash;
       }
    }
    for my $hash2 (@$hashes2) {
        my $hash = $db->query('select number,workflow_id,workflow_type,agree_user,item from daiban where workflow_type=2 and workflow_id=? and (creator=? or agree_user=?)',$hash2->{workflow_id},$creator,$creator)->hash;
        if ($hash->{number}) {
           push @array,$hash;
       }
    }
    for my $hash3 (@$hashes3) {
        my $hash = $db->query('select number,workflow_id,workflow_type,agree_user,item from daiban where workflow_type=3 and workflow_id=? and (creator=? or agree_user=?)',$hash3->{workflow_id},$creator,$creator)->hash;
        if ($hash->{number}) {
           push @array,$hash;
       }
    }

    if ($array[0]) {
        return $self->render(json => { value => 1, info => \@array });
    }else{
        return $self->render(json => { value => 0 });
    }

}

sub get_daiban {
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;

    #my $agree_user = 'ceshiyonghu'; #此为测试信息，正式系统中请使用如下语句：
    my $agree_user = $self->session('user');

    my $hashes = $db->query('select number,workflow_id,workflow_type,creator,item from daiban where is_finish=0 and agree_user=?',$agree_user)->hashes;

    if ($hashes) {
        $self->render(json => { value => 1, info => $hashes });
    }else{
        $self->render(json => { value => 0 });
    }

}

sub get_companyapplication_info {
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;
    my $issue_id = $self->param('issue_id');
    my $hashes = $db->query('select id,name,address,tel,sbu,role,operation from company_application where issue_id=?',$issue_id)->hashes;

    $self->render(json => $hashes);
}

sub input_company_info {
    my $self =shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;
    my $data = $self->req->body; #获取post的json值
    
    #my $user = 'admin'; #此为测试信息，正式系统中请使用如下语句：
    my $user = $self->session('user');
    my $hash = decode_json $data; #JSON值转换为perl的散列

=pod
input_company_info中JSON数据格式如下：

issueCompanyInfo:{
    item: "",
    workflow_id: "",
    companyInfo:[{
        id:"",
        name:"",
        address:"",
        tel:"",
        sbu:"",
        role:"",
        operation:""
    },
    ...]
}

=cut
   #验证公司注册信息
   my (@companyerror);
   my $companyinfo = $hash->{companyInfo};

   for my $hash (@$companyinfo) {
=pod
       if ($hash->{operation} =~ /1/) {
           my $array03 = $db->query('select name from companylist where id=?',$hash->{id})->array;
           if ($array03->[0]) {
               my $name = $hash->{name};
               push @companyerror,'公司名称为"'.$name.'"的MDM编码已注册';
           }
       }
=cut
       if ($hash->{operation} =~ /0/) {
           my $array04 = $db->query('select name from companylist where id=?',$hash->{id})->array;
           my $name = $hash->{name};
           if (!$array04->[0] || $array04->[0] ne $name) {
               
               push @companyerror,'公司名称为"'.$name.'"不存在或MDM编码错误';
           }
       }  else {
           if ($hash->{id} =~ /.*\s+.*/ || $hash->{name} =~ /.*\s+.*/) {
               push @companyerror,'公司名称为“'.$hash->{name}.'”编码或名称中不能存在空格';
           }
           if (!$hash->{address} || !$hash->{tel}  || !$hash->{sbu} || !$hash->{operation}) {
               push @companyerror,'公司名称为“'.$hash->{name}.'”地址、电话、所属事业部或操作类别存在未填写的情况';
           }
           if (!$hash->{role}) {
               push @companyerror,'公司名称为"'.$hash->{name}.'"应填写类别：经销商、服务商或供应商';
           }
       }
   }

   if($companyerror[0]) {
       my $errorinfo = join ';',@companyerror;
       return $self->render( json => { haserror => 1, errorinfo => $errorinfo } );
   }


   my ($wf);
   if($hash->{workflow_id}) {
       $wf = FACTORY->fetch_workflow( 'CompanyInfo',$hash->{workflow_id} );
   } else {
       $wf = FACTORY->create_workflow( 'CompanyInfo' );
   }

   $wf->context->param( db => $db );
   $wf->context->param( current_user => $user );
   $wf->context->param( company_infos => $hash->{companyInfo} );
   $wf->context->param( title => $hash->{item} );

   $wf->execute_action( 'create_company' ) or return $self->render( json => { success => 0 } );

   return $self->render( json => { success => 1 } );

}

sub agree_companyinfo {
=pod
1.获得workflow的id号
=cut
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;

    #my $user = 'ceshiyonghu'; #此为测试数据，正式系统应使用：
    my $user = $self->session('user');

    my $workflow_id = $self->param('workflow_id');
    my $wf = FACTORY->fetch_workflow( 'CompanyInfo',$workflow_id );

    $wf->context->param( db => $db );
    $wf->context->param( current_user => $user );
    $wf->context->param( agree_user => $user );
    $wf->context->param( workflow_type => 1 );

    $wf->execute_action('agree_company') or $self->render( json => { success => 0 } );

    $self->render( json => { success => 1 } );

}

sub disagree_companyinfo {
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;
    
    #my $user = 'ceshiyonghu'; #此为测试数据，正式系统应使用：
    my $user = $self->session('user');

    my $workflow_id = $self->param('workflow_id');
    my $comment = $self->param('comment');
    my $wf = FACTORY->fetch_workflow( 'CompanyInfo',$workflow_id );

    $wf->context->param( db => $db );
    $wf->context->param( agree_user => $user );
    $wf->context->param( current_user => $user );
    $wf->context->param( workflow_type => 1 );
    $wf->context->param( comment => $comment );

    $wf->execute_action('disagree_issue') or $self->render( json => { success => 0 } );

    $self->render( json => { success => 1 } );

}

sub get_flowmanage {
=pod
=get_flowmanage
1.如果url"/get_flowmanage"不带参数，向前端返回流程列表
2.如果带参数，要求如下：
  (1)operatetype: 0-获取数据，1-新增数据，2-删除数据；
  (2)number: 流程序列号
  (3)creator: 申请人
  (4)agreeuser:审批人
  (5)manager:批准人
=cut

    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;

    my ($operatetype,$number,$creator,$agreeuser,$manager);

    $operatetype = $self->param('operatetype') || 0;
    $number = $self->param('number') || '';
    $creator = $self->param('creator') || '';
    $agreeuser = $self->param('agreeuser') || '';
    $manager = $self->param('manager') || '';

    if($operatetype == 1){
        $db->query('insert into flow_manager (creator,agree_user,manager) values (?,?,?)',$creator,$agreeuser,$manager);
    }
    
    if($operatetype == 2){
        $db->query('update flow_manager set creator=?,agree_user=?,manager=? where number=?',
                    $creator,$agreeuser,$manager,$number);
    }
    
    my $admin = $self->session('user');
    my $sbu_id = $db->query('select sbu_id,adminrole from admin_account where username=?',$admin)->array;

    if ($sbu_id->[1] eq 'manager') {
      my $hashes = $db->query('select * from flow_manager')->hashes;
      return $self->render( json => $hashes);
    }

    my $users = $db->query('select username from admin_account where sbu_id=?',$sbu_id->[0])->arrays;

    my $userstring;
    for my $user (@$users) {
      $userstring = "select * from flow_manager where creator='".$user->[0]."'"."or agree_user='".$user->[0]."'" if not $userstring;
      $userstring = $userstring."or creator='".$user->[0]."'"."or agree_user='".$user->[0]."'";
    }

    my $hashes = $db->query($userstring)->hashes;
    
    $self->render( json => $hashes );
    
}

sub get_application_info {
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;

    my $workflow_id = $self->param('workflowId');
    my $workflow_type = $self->param('workflowType');
    my $issue_id = $self->param('applicationNumber');

    my $wf = FACTORY->fetch_workflow($workflow_type,$workflow_id);

    #获取审批记录

    my @h;

    foreach my $h ( $wf->get_history() ){
        push @h,{ user => $h->user,
                  date => $h->date->strftime('%Y-%m-%d %H:%M'),
                  #date => $h->date,
                  state => $h->state,
                  action => decode('utf8',$h->action),
                  description => decode('utf8',$h->description)
                };
    }
    
    #获取流程状态
    my ($state,$isediting);
    if ($wf->state =~ /INITIAL/) {
        $state = 1;
        $isediting = \1;
    };
    if ($wf->state =~ /SHENPI/) {
        $state = 2;
        $isediting = \0;
    };
    if ($wf->state =~ /JIeSHU/) {
	    $state = 3;
        $isediting = \0;
    };

    #获取申请单信息
    my ($hashes,@company_array,%check_hash);
    $hashes = $db->query('select id,name,address,tel,sbu,operation,role from company_application where issue_id=?',$issue_id)->hashes if $workflow_type =~ /CompanyInfo/;
    
    $hashes = $db->query('select mac,company_id,sbu,operation from mac_application where issue_id=?',$issue_id)->hashes if $workflow_type =~ /MacInfo/;

    $hashes = $db->query('select username,company_id,password,sbu,operation from account_application where issue_id=?',$issue_id)->hashes if $workflow_type =~ /AccountInfo/;

    for my $companyhash (@$hashes) {
        my $hash01 = $db->query('select id,name from companylist where id=?',$companyhash->{company_id})->hash;
        push @company_array,$hash01 unless $check_hash{"$hash01->{id}"};
        $check_hash{"$hash01->{id}"} = $hash01->{name};
    }
    
    #my $hases = $db->query('select id,name,address,tel,sbu,operation,role from ? where issue_id=?',$table,$issue_id)->hashes;

    $self->render( json => { history => \@h, state => $state, companyinfos => $hashes, isEditing => $isediting, companyList => \@company_array } );
}

sub get_accountapplication_info {
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;
    my $issue_id = $self->param('issue_id');
    my $hashes = $db->query('select username,company_id,password,sbu,operation from account_application where issue_id=?',$issue_id)->hashes;

    $self->render(json => $hashes);
}

sub input_account_info {
    my $self =shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;
    my $data = $self->req->body; #获取post的json值
    
    #my $user = 'admin'; #此为测试信息，正式系统中请使用如下语句：
    my $user = $self->session('user');

	my $hash = decode_json $data; #JSON值转换为perl的散列

=pod
input_company_info中JSON数据格式如下：

issueAccountInfo:{
    item: "",
    workflow_id: "",
    companyInfo:[{
        username:"",
        password:"",
        company_id:"",
        sbu:"",
        operation:""
    },
    ...]
}

=cut

   #验证账户注册信息
   my (@accounterror);
   my $accountinfo = $hash->{accountInfo};

   for my $hash (@$accountinfo) {
       if ($hash->{operation} =~ /1/) {
           if (!$hash->{company_id} || !$hash->{password} || !$hash->{sbu}){
               push @accounterror,'账户名称为"'.$hash->{username}.'"所在公司、密码或所属事业部未填写';
           }
           if ($hash->{username} =~ /.*\s+.*/) {
               push @accounterror,'账户名称为"'.$hash->{username}.'"账户名称不允许空格';
           }
           my $array04 = $db->query('select company_id from vpn_account where username=?',$hash->{username})->array;
           if ($array04->[0]) {
               my $name = $hash->{username};
               push @accounterror,'账户名称为"'.$name.'"已注册';
           }
       }
       if ($hash->{operation} =~ /[02]/) {
           my $array05 = $db->query('select company_id from vpn_account where username=?',$hash->{username})->array;
           my $name = $hash->{username};
           if (!$array05->[0]) {
               push @accounterror,'账户名称为"'.$name.'"不存在';
           } elsif ($array05->[0] ne $hash->{company_id}) {
               push @accounterror,'账户名称为"'.$name.'"所属公司错误';
           }
       }
   }

   if($accounterror[0]) {
       my $errorinfo = join ';',@accounterror;
       return $self->render( json => { haserror => 1, errorinfo => $errorinfo } );
   }


   
   my ($wf);
   if($hash->{workflow_id}) {
       $wf = FACTORY->fetch_workflow( 'AccountInfo',$hash->{workflow_id} );
   } else {
       $wf = FACTORY->create_workflow( 'AccountInfo' );
   }

   $wf->context->param( db => $db );
   $wf->context->param( current_user => $user );
   $wf->context->param( account_infos => $hash->{accountInfo} );
   $wf->context->param( title => $hash->{item} );

   $wf->execute_action( 'create_account' ) or return $self->render( json => { success => 0 } );

   $self->render( json => { success => 1 } );

}

sub agree_accountinfo {
=pod
1.获得workflow的id号
=cut
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;

    #my $user = 'ceshiyonghu'; #此为测试数据，正式系统应使用：
    my $user = $self->session('user');

    my $workflow_id = $self->param('workflow_id');
    my $wf = FACTORY->fetch_workflow( 'AccountInfo',$workflow_id );

    my $creator = $db->query('select creator from daiban where workflow_id=? and workflow_type=2',$wf->id)->array->[0];

    $wf->context->param( db => $db );
    $wf->context->param( current_user => $user );
    $wf->context->param( creator => $creator );
    $wf->context->param( workflow_type => 2 );

    $wf->execute_action('agree_account') or return $self->render( json => { success => 0 } );

    $self->render( json => { success => 1 } );

}

sub disagree_accountinfo {
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;
    
    #my $user = 'ceshiyonghu'; #此为测试数据，正式系统应使用：
    my $user = $self->session('user');

    my $workflow_id = $self->param('workflow_id');
    my $comment = $self->param('comment');
    my $wf = FACTORY->fetch_workflow( 'AccountInfo',$workflow_id );

    $wf->context->param( db => $db );
    $wf->context->param( agree_user => $user );
    $wf->context->param( current_user => $user );
    $wf->context->param( workflow_type => 2 );
    $wf->context->param( comment => $comment );

    $wf->execute_action('disagree_issue') or return $self->render( json => { success => 0 } );

    $self->render( json => { success => 1 } );

}

sub get_macapplication_info {
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;
    my $issue_id = $self->param('issue_id');
    my $hashes = $db->query('select mac,company,sbu,operation from mac_application where issue_id=?',$issue_id)->hashes;

    $self->render(json => $hashes);
}

sub input_mac_info {
    my $self =shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;
    my $data = $self->req->body; #获取post的json值
    
    #my $user = 'admin'; #此为测试信息，正式系统中请使用如下语句：
    my $user = $self->session('user');
    #查询用户所属事业部：  *****2019-4-12*****
    my $sbu_id = $db->query('select sbu_id from admin_account where username=?',$user)->array->[0];

	my $hash = decode_json $data; #JSON值转换为perl的散列

=pod
input_mac_info中JSON数据格式如下：

issueMacInfo:{
    item: "",
    workflow_id: "",
    companyInfo:[{
        username:"",
        password:"",
        company_id:"",
        sbu:"",
        operation:""
    },
    ...]
}

=cut
   #增加MAC地址和账户比例不能超过三个的验证,MAC地址不能增加已有的

   my (%companyhash,@macerror);
   my $macinfo = $hash->{macInfo};
   foreach my $machash (@$macinfo) {
       my $companyid = $machash->{company_id};

       if ($machash->{operation} =~ /1/) {
           if (!$machash->{company_id} || !$machash->{sbu}) {
               push @macerror,"MAC地址".$machash->{mac}."所属公司或所属事业部未填写";
           }
           my $array02 = $db->query('select company from maclist where mac=?',$machash->{mac})->array;
           if ($array02->[0]) {
               my $mac = $machash->{mac};
               push @macerror,"MAC地址".$mac."已经注册";
           } else {
                if (!exists $companyhash{$companyid}) {
                    $companyhash{$companyid} = 1;
                } else {
                    ++$companyhash{$companyid};
                }
           }
       }
       if ($machash->{operation} =~ /0/) {
           my $array06 = $db->query('select company from maclist where mac=?',$machash->{mac})->array;
           if (!$array06->[0] || $array06->[0] ne $companyid) {
               my $mac = $machash->{mac};
               push @macerror,"MAC地址".$mac."不存在或绑定的公司错误";
           } else {
               if (!exists $companyhash{$companyid}) {
                   $companyhash{$companyid} = -1;
               } else {
                   --$companyhash{$companyid};
               }
           }
       }
       if ($machash->{operation} =~ /2/){
         my $array = $db->query('select company from maclist where mac=?', $machash->{mac})->array;
         if (!$array->[0]){
           my $mac = $machash->{mac};
           push @macerror,"MAC地址".$mac."不存在，不能转移";
         } else {
           if (!exists $companyhash{$companyid}) {
             $companyhash{$companyid} = 1;
           } else {
             ++$companyhash{$companyid};
           }
         }
       }
   }

   foreach my $key (keys %companyhash) {
       my $array = $db->query('select username from vpn_account where company_id=?',$key)->arrays;
       if (!$array->[0]->[0]) {
           #my $string = "公司编码为".$key."的公司必须先申请至少1个VPN账户";
           push @macerror,"公司编码为".$key."的公司必须先申请至少1个VPN账户";
           next;
       }
       my $i = @$array || 0;
       my $array01 = $db->query('select mac from maclist where company=?',$key)->arrays;
       my $b = 0;
       if ($array01->[0]->[0]) {
           $b = @$array01;
       }
       next if ($sbu_id !~/002|011/);
       next if ($companyhash{$key} <= 0 || $companyhash{$key}+$b <= $i*3);
       #my $string = "公司编码为".$key."的公司已有账户".$b."个最多允许3倍数量的地址";
       push @macerror,"公司编码为".$key."的公司已有账户".$i."个最多允许3倍数量的地址，已有MAC地址".$b.'个';
   }
   if($macerror[0]) {
       my $errorinfo = join ';',@macerror;
       return $self->render( json => { haserror => 1, errorinfo => $errorinfo } );
   }


   #如果申请单MAC地址数量符合要求
   
   my ($wf);
   if($hash->{workflow_id}) {
       $wf = FACTORY->fetch_workflow( 'MacInfo',$hash->{workflow_id} );
   } else {
       $wf = FACTORY->create_workflow( 'MacInfo' );
   }

   $wf->context->param( db => $db );
   $wf->context->param( current_user => $user );
   $wf->context->param( mac_infos => $hash->{macInfo} );
   $wf->context->param( title => $hash->{item} );

   $wf->execute_action( 'create_mac' ) or return $self->render( json => { success => 0 } );

   $self->render( json => { success => 1 } );

}

sub agree_macinfo {
=pod
1.获得workflow的id号
=cut
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;

    #my $user = 'ceshiyonghu'; #此为测试数据，正式系统应使用：
    my $user = $self->session('user');

    my $workflow_id = $self->param('workflow_id');
    my $wf = FACTORY->fetch_workflow( 'MacInfo',$workflow_id );

    my $creator = $db->query('select creator from daiban where workflow_id=? and workflow_type=3',$wf->id)->array->[0];

    $wf->context->param( db => $db );
    $wf->context->param( current_user => $user );
    $wf->context->param( creator => $creator );
    $wf->context->param( workflow_type => 3 );
    $wf->execute_action('agree_mac') or return $self->render( json => { success => 0 } );

    $self->render( json => { success => 1 } );

}

sub disagree_macinfo {
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;
    
    #my $user = 'ceshiyonghu'; #此为测试数据，正式系统应使用：
    my $user = $self->session('user');

    my $workflow_id = $self->param('workflow_id');
    my $comment = $self->param('comment');
    my $wf = FACTORY->fetch_workflow( 'MacInfo',$workflow_id );

    $wf->context->param( db => $db );
    $wf->context->param( agree_user => $user );
    $wf->context->param( current_user => $user );
    $wf->context->param( workflow_type => 3 );
    $wf->context->param( comment => $comment );

    $wf->execute_action('disagree_issue') or return $self->render( json => { success => 0 } );

    $self->render( json => { success => 1 } );

}

sub searchcompany {
    my $self = shift;
    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	#$self->res->headers->access_control_allow_origin('*'); #测试代码，上线前删除
	#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    my $db = $self->mysql->db;

    my $keyword = $self->param('keyword');

    my $word = "%".$keyword."%";

    my $hashes = $db->query('select id,name from companylist where name like ? and enable=1',$word)->hashes;

    $self->render( json => $hashes );
}

1;
